# __author:wanghao
# data:2022/4/6
import time

from case.base.basepage import BasePage
from case.pages.storepage import StorePage
from case.base import route

"""
购物车页面公共方法
"""


class CartPage(BasePage):
    # 页面公共元素判断，包含view元素
    locators = {
        "BASE_ELEMENT": "view",
        "BASE_BANNER": "view[class='cart-title']"
    }

    """
    跳转到购物车页面,该页面是tabbar页面，不能以导航的方式跳转
    """

    def switch_tab_open_cart(self):
        self.switch_tab_open(route.cartpage_route)

    """
    校验页面路径
    """

    def check_page_path(self, route_path):
        self.mini.assertEqual(self.current_path(), route_path, "当前页面路径不符合预期")

    """
    校验页面的基本元素
    """

    def check_cart_page_base_element(self):
        # 校验页面是否包含view元素
        self.mini.assertTrue(self.mini.page.element_is_exists(CartPage.locators['BASE_ELEMENT']))
        self.mini.assertTrue(self.mini.page.element_is_exists(CartPage.locators['BASE_BANNER']))

    """
    一键清空购物车
    """

    def clear_cart(self):
        result = self.mini.page.wait_for("view.go-shopping-btn", 3)  # 等待页面渲染完成
        if not result:
            # 点击编辑
            edit_cart_button = self.mini.page.get_element("text.edit")
            edit_cart_button.click()
            # 全选
            select_all_button = self.mini.page.get_element("view.checkbox-icon.checkbox-icon--default")
            select_all_button.click()
            # delete button
            delete_button = self.mini.page.get_element("view.submit-btn")
            delete_button.click()
            # self.mini.assertTrue(self.mini.page.element_is_exists("view.go-shopping-btn"))
        return self

    """
    删除购物车第一个第N个商品
    """

    def delete_one_cart(self, index=0):

        result = self.mini.page.wait_for("view.go-shopping-btn", 2)  # 等待页面渲染完成
        if not result:
            # 点击编辑
            goods_exists = self.mini.page.wait_for("view[class='single-goods'][data-index='%d']" % index, 3)
            if goods_exists:
                goods_name = self.mini.page.get_elements("view[class='single-goods']")
                # 滑动商品
                touch, changed_touch, move_delay = BasePage.move_touch_aiguang(self,goods_name[index], -100, 0, 350)
                goods_name[index].touch_move(touches=[touch], changed_touches=[changed_touch])
                time.sleep(move_delay / 2000)
                goods_name[index].touch_end(changed_touches=[changed_touch])
                # 删除商品
                delete_button = self.mini.page.get_element("view.delete-text")
                delete_button.click()
        return self

    """
    删除购物车第一个第N个商品
    """

    def delete_all_cart(self):
        result = self.mini.page.wait_for("view.go-shopping-btn", 2)  # 等待页面渲染完成
        while not result:
            # 点击编辑
            goods_exists = self.mini.page.wait_for("view[class='single-goods'][data-index='0']", 3)
            if goods_exists:
                goods_name = self.mini.page.get_elements("view[class='single-goods']")
                # 滑动商品
                touch, changed_touch, move_delay = BasePage.move_touch_aiguang(self, goods_name[0], -100, 0, 350)
                goods_name[0].touch_move(touches=[touch], changed_touches=[changed_touch])
                time.sleep(move_delay / 2000)
                goods_name[0].touch_end(changed_touches=[changed_touch])
                # 删除商品
                delete_button = self.mini.page.get_element("view.delete-text")
                delete_button.click()
            else:
                break
        return self

    """
    加入购物车校验
    """

    def check_add_to_cart(self):
        StorePage.to_store_good_detail_page(self)
        time.sleep(2)
        result = self.mini.page.wait_for("view.navigator-index--pretty-navigator__info", 3)  # 等待页面渲染完成
        cart_number = 0
        if result:
            cart_number = self.mini.page.get_element("view.navigator-index--pretty-navigator__info").inner_text
        # 加购
        add_to_cart_button = self.mini.page.get_element(
            "button.components-action-index--e-core-btn.components-action-index--e-core-btn__vice")
        add_to_cart_button.click()
        time.sleep(1)
        add_to_cart_confirm_button = self.mini.page.get_element(
            "button.operation-index--e-core-btn.operation-index--e-core-btn__single")
        add_to_cart_confirm_button.click()
        # 校验加购
        time.sleep(2)
        added_cart_number = self.mini.page.get_element("view.navigator-index--pretty-navigator__info").inner_text
        assert int(added_cart_number) == (int(cart_number) + 1)
        return self

    """
    商详页进入购物车
    """

    def to_cart_from_goods_detail(self):
        # 商详页进入购物车
        into_cart_button = self.mini.page.get_elements("view.navigator-index--pretty-navigator__img-wrap")
        into_cart_button[1].click()
        # 校验进入购物车成功
        self.mini.assertTrue(self.mini.page.element_is_exists("view.goods-name.van-multi-ellipsis--l2"))
        return self

    """
    购物车购买商品
    """

    def check_cart_shopping_flow(self):

        # 选中最后一个商品
        check_boxes = self.mini.page.get_elements("view.checkbox-icon.checkbox-icon--default")
        check_boxes[0].click()
        # 结算
        self.mini.page.get_element("view.submit-btn").click()
        StorePage.submit_goods_buy(self)
        return self
